package com.ai.controller.reviewed;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.common.controller.BaseController;
import com.ai.mmd.reviewed.dto.SbhzinfoDTO;
import com.ai.mmd.reviewed.dto.StatisticsDTO;
import com.ai.mmd.reviewed.dto.StatisticsHwlxDTO;
import com.ai.mmd.reviewed.vo.HotmapVO;
import com.ai.mmd.reviewed.vo.SbhzinfoVO;
import com.ai.mmd.reviewed.vo.StatisticsHwlxVO;
import com.ai.mmd.reviewed.vo.StatisticsVO;
import com.ai.service.reviewed.service.ISbhzinfoService;

/**
 * 统计
 */
@RequestMapping("statistics")
@Controller("com.ai.controller.reviewed.StatisticsController")
public class StatisticsController extends BaseController {
	
	/**
	 * 跳转
	 * 
	 * @date 2016年6月26日 上午11:29:33
	 * @param name
	 * @return
	 * @throws Exception
	 *             String
	 */
	@RequestMapping("forward/{name}")
	public String forward(@PathVariable String name) throws Exception {
		return "/order/" + name;
	}
	
	/**
     * 统计箱量
     *
     * @param statisticsDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("xlStatistics")
    public Map<String, Object> xlStatistics(
            @ModelAttribute StatisticsDTO statisticsDTO) throws Exception {
        List<String> categories = new ArrayList<String>();
        categories.add("第一季度");
        categories.add("第二季度");
        categories.add("第三季度");
        categories.add("第四季度");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (statisticsDTO.getNowyear()==null||"".equals(statisticsDTO.getNowyear())) {
        	Calendar a=Calendar.getInstance();
        	statisticsDTO.setNowyear(String.valueOf(a.get(Calendar.YEAR)));
		}
        List<StatisticsVO> list = isbhzinfoService.getXlStatistics(statisticsDTO);
        resultMap.put("list", list);
        resultMap.put("categories", categories);
        return resultMap;
    }
    
    /**
     * 货物类型统计
     *
     * @param statisticsDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("hwlxStatistics")
    public Map<String, Object> hwlxStatistics(
            @ModelAttribute StatisticsHwlxDTO statisticsHwlxDTO) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<StatisticsHwlxVO> list = isbhzinfoService.getHwlxStatistics(statisticsHwlxDTO);
        List<String> categories =new ArrayList<String>();
        List<Integer> countList = new ArrayList<Integer>();
        Integer count = 0;
        for (StatisticsHwlxVO statisticsHwlxVO : list) {
        	categories.add(statisticsHwlxVO.getHwlxmc());
        	countList.add(statisticsHwlxVO.getCount());
        	count += statisticsHwlxVO.getCount();
		}
        resultMap.put("categories", categories);
        resultMap.put("countList", countList);
        resultMap.put("listSize", count);
        return resultMap;
    }
    
    /**
     * 统计起点终点
     *
     * @param statisticsDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("fromtoStatistics")
    public Map<String, Object> fromtoStatistics(
            @ModelAttribute StatisticsDTO statisticsDTO) throws Exception {
        List<String> categories = new ArrayList<String>();  
        categories.add("第四季度");
        categories.add("第三季度");
        categories.add("第二季度");
        categories.add("第一季度");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        if (statisticsDTO.getNowyear()==null||"".equals(statisticsDTO.getNowyear())) {
        	Calendar a=Calendar.getInstance();
        	statisticsDTO.setNowyear(String.valueOf(a.get(Calendar.YEAR)));
		}
        List<StatisticsVO> list = isbhzinfoService.getfromtoStatistics(statisticsDTO);
        
        //摘取类型
        List<String> strlist = new ArrayList<String>();
        if (list.size()>0) {
			for (StatisticsVO statisticsVO : list) {
				if (!strlist.contains(statisticsVO.getType())) {
					strlist.add(statisticsVO.getType());
				}
			}
		}
        
        resultMap.put("strlist", strlist);
        resultMap.put("list", list);
        resultMap.put("categories", categories);
        return resultMap;
    }
    
    /**
     * 统计起点终点
     *
     * @param statisticsDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("fromtoStatisticsbar")
    public Map<String, Object> fromtoStatisticsbar(
            @ModelAttribute StatisticsDTO statisticsDTO) throws Exception {
        List<String> categories = new ArrayList<String>();
        if (statisticsDTO.getSbjd()==1) {
        	categories.add("第一季度");
		}else if (statisticsDTO.getSbjd()==2) {
			categories.add("第二季度");
		}else if (statisticsDTO.getSbjd()==3) {
			categories.add("第三季度");
		}else {
			categories.add("第四季度");
		}
               
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        if (statisticsDTO.getNowyear()==null||"".equals(statisticsDTO.getNowyear())) {
        	Calendar a=Calendar.getInstance();
        	statisticsDTO.setNowyear(String.valueOf(a.get(Calendar.YEAR)));
		}
        List<StatisticsVO> list = isbhzinfoService.getfromtoStatistics(statisticsDTO);
        
        //摘取类型
        List<String> strlist = new ArrayList<String>();
        if (list.size()>0) {
			for (StatisticsVO statisticsVO : list) {
				if (!strlist.contains(statisticsVO.getType())) {
					strlist.add(statisticsVO.getType());
				}
			}
		}
        
        resultMap.put("strlist", strlist);
        resultMap.put("list", list);
        resultMap.put("categories", categories);
        return resultMap;
    }
    
    /**
     * 统计起点终点
     *
     * @param statisticsDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("fromtoStatisticsLine")
    public Map<String, Object> fromtoStatisticsLine(
            @ModelAttribute StatisticsDTO statisticsDTO) throws Exception {
        List<String> categories = new ArrayList<String>();  
        categories.add("第一季度");
        categories.add("第二季度");
        categories.add("第三季度");
        categories.add("第四季度");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        if (statisticsDTO.getNowyear()==null||"".equals(statisticsDTO.getNowyear())) {
        	Calendar a=Calendar.getInstance();
        	statisticsDTO.setNowyear(String.valueOf(a.get(Calendar.YEAR)));
		}
        List<StatisticsVO> list = isbhzinfoService.getfromtoStatistics(statisticsDTO);
        
        //摘取类型
        List<String> strlist = new ArrayList<String>();
        if (list.size()>0) {
			for (StatisticsVO statisticsVO : list) {
				if (!strlist.contains(statisticsVO.getType())) {
					strlist.add(statisticsVO.getType());
				}
			}
		}
        
        resultMap.put("strlist", strlist);
        resultMap.put("list", list);
        resultMap.put("categories", categories);
        return resultMap;
    }
    
    /**
     * 首页图表统计
     *
     * @param statisticsDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("statisticsForIndex")
    public Map<String, Object> statisticsForIndex(@RequestParam(required = true) Integer flag) throws Exception {
        List<String> categories = new ArrayList<String>();
        Map<Object, Integer> map = new HashMap<Object, Integer>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        map.put("flag", flag);
        List<SbhzinfoVO> list = isbhzinfoService.getStatisticsForIndex(map);
        for (SbhzinfoVO sbhzinfoVO : list) {
        	if (!categories.contains(sbhzinfoVO.getSbsj())) {
        		categories.add(sbhzinfoVO.getSbsj());
			}
		}
        resultMap.put("list", list);
        resultMap.put("categories", categories);
        return resultMap;
    }
    
    /**
     * 总统计箱量
     *
     * @param statisticsDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("zxlStatistics")
    public Map<String, Object> zxlStatistics(
            @ModelAttribute StatisticsDTO statisticsDTO) throws Exception {
        List<String> categories = new ArrayList<String>();
        categories.add("第一季度");
        categories.add("第二季度");
        categories.add("第三季度");
        categories.add("第四季度");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Integer nowyear = 0;
        if (statisticsDTO.getNowyear()==null||"".equals(statisticsDTO.getNowyear())) {
        	Calendar a=Calendar.getInstance();
        	nowyear = a.get(Calendar.YEAR);
        	
		}else {
			nowyear = Integer.parseInt(statisticsDTO.getNowyear());
		}
        
        List<StatisticsVO> firstlist = new ArrayList<StatisticsVO>();
        List<StatisticsVO> secondlist = new ArrayList<StatisticsVO>();
        String yearlist[] = {String.valueOf(nowyear-1),String.valueOf(nowyear)};
        
        //第一年
        statisticsDTO.setNowyear(String.valueOf(nowyear-1));
        firstlist = isbhzinfoService.getXlStatistics(statisticsDTO);
        
        //第二年
        statisticsDTO.setNowyear(String.valueOf(nowyear));
        secondlist = isbhzinfoService.getXlStatistics(statisticsDTO);
        
        resultMap.put("yearlist", yearlist);
        resultMap.put("firstlist", firstlist);
        resultMap.put("secondlist", secondlist);
        resultMap.put("categories", categories);
        return resultMap;
    }
    
    /**
     * 热力箱量
     *
     * @param statisticsDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("zxlhotmap")
    public Map<String, Object> zxlhotmap(
            @ModelAttribute SbhzinfoDTO sbhzinfoDTO) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        sbhzinfoDTO.setHzzt(3);
        List<HotmapVO> hotmaplist = isbhzinfoService.getHotmapList(sbhzinfoDTO);
        
        resultMap.put("hotmaplist", hotmaplist);

        return resultMap;
    }
	
	@Resource(name = "isbhzinfoService")
	private ISbhzinfoService isbhzinfoService;
}
